"""
File: student_management_system.py
Author: ershixiong
Date: 2023/12/19
describe:

"""


class MyException(Exception):
    def __init__(self, msg):
        super().__init__(msg)


def verify_input_int_type(value):
    if not isinstance(value, int):
        try:
            return int(value)
        except:
            raise MyException(f'输入的内容：{value} ，不是整数')


class Student:
    def __init__(self, id, name, sex):
        self.id = id
        self.name = name
        self.sex = sex


class StudentManagement:
    def __init__(self):
        self.student_pool = []

    def addStudent(self, id, name, sex):
        self.stu_new = Student(id, name, sex)
        self.student_pool.append(self.stu_new)
        print(f'添加学员成功，该学员信息为：')
        print(f'学号：{self.stu_new.id}，姓名：{self.stu_new.name}，性别：{self.stu_new.sex}')

    def main(self, id, **kwargs):
        student = next(student for student in self.student_pool if student.id == id)
        for k, v in kwargs.items():
            student.k = v

    def getStudents(self):
        if len(self.student_pool) > 0:
            for student in self.student_pool:
                print(f'学号：{student.id}，姓名：{student.name}，性别：{student.sex}')
        else:
            print('暂未添加学生，请先添加学生!')

    def detailStudent(self, id):
        goal = False
        if len(self.student_pool) > 0:
            for student in self.student_pool:
                if student.id == id:
                    goal = True
                    print('学员信息获取成功，该学员信息为：')
                    print(f'学号：{student.id}，姓名：{student.name}，性别：{student.sex}')
                    break
            if not goal:
                print('未查询到对应学号的学生，请确保学号正确!')
        else:
            print('暂未添加学生，请先添加学生!')

    def deleteStudent(self, id):
        if len(self.student_pool) > 0:
            # goal = False
            # for student in self.student_pool:
            #     if student.id == id:
            #         goal = True
            #         self.student_pool.remove(student)
            #         print(f'删除成功，删除的学员信息为：学号:{student.id}，姓名:{student.name}，性别:{student.sex}')
            #         print('删除后的学员信息为：')
            #         self.getStudents()
            #         break
            # if not goal:
            #     print('未查询到对应学号的学生，请确保学号正确!')

            try:
                student = next(student for student in self.student_pool if student.id == id)
                self.student_pool.remove(student)
                print(f'删除成功，删除的学员信息为：学号:{student.id}，姓名:{student.name}，性别:{student.sex}')
                print('删除后的学员信息为：')
                self.getStudents()
            except:
                print('未查询到对应学号的学生，请确保学号正确!')
        else:
            print('暂未添加学生，请先添加学生!')


def main():
    print("""
    ----------------------欢迎来到学员信息管理系统----------------
            1、根据学号查看学员信息
            2、添加学员
            3、根据学号删除学员后，查看所有学员信息
            4、查询当前所有学员信息
            5、退出系统
    """)

    manage = StudentManagement()

    while True:
        try:
            input_num = input('请输入你的选择：')
            verify_input_int_type(input_num)
            input_num = verify_input_int_type(input_num)

        except MyException as e:
            print(e)

        if input_num == 1:
            student_id = input('请输入想要查找的学员编号：')
            manage.detailStudent(student_id)
        elif input_num == 2:
            student_id = input('请输入学员编号：')
            student_name = input('请输入学员姓名：')
            student_sex = input('请输入学员性别：')
            manage.addStudent(student_id, student_name, student_sex)
        elif input_num == 3:
            student_id = input('请输入想要删除的学员编号：')
            manage.deleteStudent(student_id)
        elif input_num == 4:
            manage.getStudents()
        elif input_num == 5:
            print('成功退出系统，欢迎下次使用')
            break
        else:
            print('请输入1-5的整数！')
            continue


if __name__ == '__main__':
    main()
